package org.hydrakyoufeng.stuplatform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.hydrakyoufeng.stuplatform.dto.ChapterDTO;
import org.hydrakyoufeng.stuplatform.dto.ChapterListDTO;
import org.hydrakyoufeng.stuplatform.dto.ChapterSortDTO;
import org.hydrakyoufeng.stuplatform.dto.ChapterUserListDTO;
import org.hydrakyoufeng.stuplatform.mapper.ChapterMapper;
import org.hydrakyoufeng.stuplatform.service.ChapterService;
import org.hydrakyoufeng.stuplatform.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ChapterServiceImpl implements ChapterService {

    private final ChapterMapper chapterMapper;

    private final ThreadLocal<Integer> adminIdTheadLocal;
    private final ThreadLocal<Integer> userIdTheadLocal;

    @Autowired
    public ChapterServiceImpl(ChapterMapper chapterMapper,
            @Qualifier("adminIdTheadLocal") ThreadLocal<Integer> adminIdTheadLocal,
            @Qualifier("userIdTheadLocal") ThreadLocal<Integer> userIdTheadLocal) {
        this.chapterMapper = chapterMapper;
        this.adminIdTheadLocal = adminIdTheadLocal;
        this.userIdTheadLocal = userIdTheadLocal;
    }
    @Override
    public ResultEntity queryList(ChapterListDTO dto) throws Exception {
        PageHelper.startPage(dto.getCurrentPage(),dto.getPageSize(),"sort desc,id");
        PageInfo pageInfo = new PageInfo(chapterMapper.queryList(dto));
        return ResultEntity.success("查询成功",pageInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultEntity updateSort(ChapterSortDTO dto) throws Exception {
        dto.setUpdate_by(adminIdTheadLocal.get());
        // 打印日志
        log.info("updateSort.chapterSortDTO = " + dto);
        var ids = dto.getIds();
        for (int i = 0; i < ids.length; i++) {
            dto.setId(ids[i]);
            dto.setSort(i);
            chapterMapper.updateSort(dto);
        }
        return ResultEntity.success("更新成功",chapterMapper.updateSort(dto));
    }

    @Override
    public ResultEntity delete(Integer id) {
        int result = chapterMapper.delete(id);
        if (result > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.error("删除失败");
        }
    }

    @Override
    public ResultEntity insert(ChapterDTO chapterDTO) throws Exception {
        chapterDTO.setUpdate_by(adminIdTheadLocal.get());
        chapterDTO.setCreator_by(adminIdTheadLocal.get());
        int result = chapterMapper.insert(chapterDTO);
        if (result > 0) {
            return ResultEntity.success("增加成功",chapterDTO.getId());
        } else {
            return ResultEntity.error("增加失败");
        }
    }

    @Override
    public ResultEntity update(ChapterDTO chapterDTO) throws Exception {
        chapterDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = chapterMapper.update(chapterDTO);
        if (result > 0) {
            return ResultEntity.success("修改成功");
        } else {
            return ResultEntity.error("修改失败");
        }
    }

    @Override
    public ResultEntity queryUserList(Integer courseId) {
        ChapterUserListDTO dto = new ChapterUserListDTO();
        dto.setCourse_id(courseId);
        dto.setUser_id(userIdTheadLocal.get());
        return ResultEntity.success("查询成功",
                chapterMapper.queryUserList(dto));
    }
}
